# +----------------------------------------------------------------------
# | ChatWork智能聊天办公系统
# +----------------------------------------------------------------------
# | 软件声明: 本系统并非自由软件,未经授权任何形式的商业使用均属非法。
# | 版权保护: 任何企业和个人不允许对程序代码以任何形式任何目的复制/分发。
# | 授权要求: 如有商业使用需求,请务必先与版权所有者取得联系并获得正式授权。
# +----------------------------------------------------------------------
# | Author: ChatWork Team <2474369941@qq.com>
# +----------------------------------------------------------------------
import os
import importlib
from typing import List
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles

__all__ = ["configure_static"]


def __loading_static_configs():
    """ Load Static configuration """
    configs = {}
    try:
        package = importlib.import_module("config")
        clz = getattr(package, "GlobalSetting", None)
        if not clz:
            return configs

        obj = clz().dict()
        static_config = obj.get("STATIC_DIR", [])
        return static_config
    except ModuleNotFoundError:
        return configs


def configure_static(app: FastAPI):
    """ Configure static resources """
    statics: List[tuple] = __loading_static_configs()
    for t in statics:
        directory: str = os.path.join(os.getcwd(), t[1])
        app.mount(t[0], StaticFiles(directory=directory), name=t[2])
